package com.tju320.elm.view.impl;

import java.util.List;
import java.util.Scanner;

import com.tju320.elm.dao.BusinessDao;
import com.tju320.elm.dao.impl.BusinessDaoImpl;
import com.tju320.elm.po.Business;
import com.tju320.elm.view.BusinessView;

public class BusinessViewImpl implements BusinessView{
	private Scanner keyboardInput = new Scanner(System.in);
	@Override
	public void listBusinessAll() {
		BusinessDao businessdao=new BusinessDaoImpl();
		List<Business> all=businessdao.listBusiness(null, null);
		System.out.println("商家列表\t商家名称\t商家地址\t商家介绍\t起送费\t配送费");
		for(Business x : all) {
			System.out.println(x.getBusinessId().toString()+"\t"+x.getBusinessName()+
					"\t"+x.getBusinessAddress()+"\t"+x.getBusinessExplain()+
					"\t"+x.getStarPrice().toString()+"\t"+x.getDeliveryPrice().toString());
		}
	}
	

	@Override
	public void listBusiness() {
		
		String s;
		String businessName=null;
		String businessAddress=null;
		System.out.println("是否需要输入商家名称关键词(y/n):");
		s=keyboardInput.next();
		if(s.equals("y")) {
			System.out.println("请输入商家名称关键词：");
			businessName=keyboardInput.next();
		}
		System.out.println("是否需要输入商家地址关键词(y/n):");
		s=keyboardInput.next();
		if(s.equals("y")) {
			System.out.println("请输入商家地址关键词：");
			businessAddress=keyboardInput.next();
		}
		BusinessDao businessdao=new BusinessDaoImpl();
		List<Business> all=businessdao.listBusiness(businessName, businessAddress);
		System.out.println("商家列表\t商家名称\t商家地址\t商家介绍\t起送费\t配送费");
		for(Business x : all) {
			System.out.println(x.getBusinessId().toString()+"\t"+x.getBusinessName()+
					"\t"+x.getBusinessAddress()+"\t"+x.getBusinessExplain()+
					"\t"+x.getStarPrice().toString()+"\t"+x.getDeliveryPrice().toString());
		} 
	}
	

	@Override
	public void saveBusiness() {
		
		String businessName;
		System.out.println("请输入商家名称：");
		businessName=keyboardInput.next();
		BusinessDao businessdao=new BusinessDaoImpl();
		Integer businessId=businessdao.saveBusiness(businessName);
		System.out.println("创建商家成功！商家编号为:"+businessId.toString());
	}

	@Override
	public void removeBusiness() {
		System.out.println("请输入商家编号:");
		int businessId=keyboardInput.nextInt();
		System.out.println("确认要删除吗(y/n):");
		String s=keyboardInput.next();
		if(s.equals("y")) {
			BusinessDao businessdao=new BusinessDaoImpl();
			if(businessdao.removeBusiness(businessId)==0) {
				System.out.println("删除失败，没有这个编号");
			}else {
				System.out.println("删除商家成功");
			}
		}
	}
	

	@Override
	public Business login() {
		System.out.println("请输入商家编号：");
		int businessId=keyboardInput.nextInt();
		System.out.println("请输入密码：");
		String password=keyboardInput.next();
		BusinessDao businessdao=new BusinessDaoImpl();
		Business business=businessdao.getBusinessByIdByPass(businessId,password);
		return business;
	}
	
	@Override
	public void showBusiness(Integer businessId) {
		BusinessDao businessdao=new BusinessDaoImpl();
		Business business=businessdao.getBusinessById(businessId);
		System.out.printf("商家编号:%d\n",business.getBusinessId());
		System.out.printf("商家名称:%s\n",business.getBusinessName());
		System.out.printf("商家地址:%s\n",business.getBusinessAddress());
		System.out.printf("商家介绍:%s\n",business.getBusinessExplain());
		System.out.printf("起送费:%.01f\n",business.getStarPrice());
		System.out.printf("配送费:%.01f\n",business.getDeliveryPrice());
		
	}
	
	

	@Override
	public void editBusiness(Integer businessId) {
		showBusiness(businessId);
		BusinessDao businessdao=new BusinessDaoImpl();
		Business business=businessdao.getBusinessById(businessId);
		
		System.out.println("是否输入商家名称(y/n):");
		String s=keyboardInput.next();
		if(s.equals("y")) {
			System.out.println("请输入新的商家名称:");
			s=keyboardInput.next();
			business.setBusinessName(s);
		}
		
		System.out.println("是否输入商家地址(y/n):");
		s=keyboardInput.next();
		if(s.equals("y")) {
			System.out.println("请输入新的商家地址:");
			s=keyboardInput.next();
			business.setBusinessAddress(s);
		}
		
		System.out.println("是否输入商家介绍(y/n):");
		s=keyboardInput.next();
		if(s.equals("y")) {
			System.out.println("请输入新的商家介绍:");
			s=keyboardInput.next();
			business.setBusinessExplain(s);
		}
		
		System.out.println("是否修改起送费(y/n):");
		s=keyboardInput.next();
		if(s.equals("y")) {
			System.out.println("请输入新的起送费:");
			double d=keyboardInput.nextDouble();
			business.setStarPrice(d);
		}
		
		System.out.println("是否修改配送费(y/n):");
		s=keyboardInput.next();
		if(s.equals("y")) {
			System.out.println("请输入新的配送费:");
			double d=keyboardInput.nextDouble();
			business.setDeliveryPrice(d);
		}
		
		if(businessdao.updateBusiness(business)>0) {
			System.out.println("修改商家信息成功");
		}
		
		
	}
	
	public static void main(String[] args) {
		new BusinessViewImpl().updateBusinessByPassword(1);
		
	}

	@Override
	public void updateBusinessByPassword(Integer businessId) {
		BusinessDao businessdao=new BusinessDaoImpl();
		Business business=businessdao.getBusinessById(businessId);
		System.out.println("请输入旧密码:");
		String s=keyboardInput.next();
		if(!s.equals(business.getPassword())) {
			System.out.println("密码输入错误!");
			return;
		}
		System.out.println("请输入新密码:");
		s=keyboardInput.next();
		System.out.println("请再次输入新密码:");
		if(!s.equals(keyboardInput.next())) {
			System.out.println("两次密码输入不一致！");
			return;
		}
		if(businessdao.updateBusinessByPassword(businessId, s)>0) {
			System.out.println("修改密码成功!");
		}else {
			System.out.println("修改密码失败!");
		}
		
		
		
	}

}
